package org.example.struct.list.basicMethod;

import com.alibaba.fastjson.JSON;
import org.example.struct.list.Node;

/**
 * @author liyishan
 * @date 2023/7/13 11:11
 * @apiNote
 */

public class insert {
    public static void main(String[] args) {
        Node<Object> head = new Node<>(1);
        head.next = new Node<>(2);
        head.next.next = new Node<>(3);
        head.next.next.next = new Node<>(4);
        head.next.next.next.next = new Node<>(5);
        head.next.next.next.next.next = new Node<>(6);
        head.next.next.next.next.next.next = new Node<>(7);
        //linkFirst(head,4);
        //deleteFirst(head);
        //linkLast(head,8);
        //deletetLast(head);
        //linkNode(head,2,9);
        deleteNode(head,2);

    }

    /**
     * 尾插法
     * @param list
     * @param o
     * 先找到尾结点，然后把尾结点的next指向新的节点
     */
    public static void linkFirst(Node<Object> list, Object o){
        Node<Object> tail = list;
        while(tail.next != null){
            tail = tail.next;
        }
        tail.next = new Node<>(null, o, null);
        System.out.println(JSON.toJSON(list));

    }

    /**
     * 尾删法
     * @param list
     */
    public static void deleteFirst(Node<Object> list){
        Node<Object> tail = list;
        Node<Object> tail2 = list;
        while(tail.next != null){
            tail = tail.next;
        }
        while(tail2.next != tail){
            tail2 = tail2.next;
        }
        tail2.next = null;
        System.out.println(JSON.toJSON(list));

    }



    /**
     * 头插法
     * @param list
     * @param o
     * 先找到头节点，然后用nextNode指向  list.next  然后让新节点的next指向nextNode  然后 list的next执行newNode
     */
    public static void linkLast(Node<Object> list, Object o){
        Node<Object> newNode = new Node<>(null, o, null);
        newNode.next = list.next;
        list.next = newNode;
        System.out.println(JSON.toJSON(list));
    }

    /**
     * 头删法
     * @param list
     */
    public static void deletetLast(Node<Object> list){
        Node<Object> nextNode = list.next;
        list.next = null;
        list = nextNode;
        System.out.println(JSON.toJSON(list));
    }


    /**
     * 在指定节点前插入某个数据
     * @param list
     * @param o
     * @param targetObject
     */
    public static void linkNode(Node<Object> list, Object o,Object targetObject){
        Node<Object> currentNode = list;
        Node<Object> preNode = list;
        while(!o.equals(currentNode.item)){
            currentNode = currentNode.next;

        }
        while(preNode.next !=  currentNode){
            preNode = preNode.next;
        }
        preNode.next = new Node<>(null, targetObject, currentNode);
        System.out.println(JSON.toJSON(list));
    }


    /**
     * 删除某个数据
     * @param list
     * @param o
     */
    public static void deleteNode(Node<Object> list, Object o){
        Node<Object> currentNode = list;
        Node<Object> preNode = list;
        while(!o.equals(currentNode.item)){
            currentNode = currentNode.next;

        }
        while(preNode.next !=  currentNode){
            preNode = preNode.next;
        }
        preNode.next = currentNode.next;
        System.out.println(JSON.toJSON(list));
    }


}
